package com.wn.class30.weixinmessageHandler;


import com.wn.class30.entity.User;
import com.wn.class30.service.UserService;
import com.wn.class30.tools.OptionidUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.weixin4j.model.message.OutputMessage;
import org.weixin4j.model.message.event.*;
import org.weixin4j.spi.IEventMessageHandler;
import org.weixin4j.spring.WeixinTemplate;

import javax.annotation.Resource;
import java.util.Date;

/*微信事件,生效后要取消 properties 对应的默认事件,关注,取消关注*/
@Component
public class EventMessageHandler implements IEventMessageHandler {
    @Resource
    private UserService userServiceImp;

    @Resource
    private WeixinTemplate weixinTemplate;

    @SneakyThrows
    @Override
    public OutputMessage subscribe(SubscribeEventMessage subscribeEventMessage) {
        String fromUserName = subscribeEventMessage.getFromUserName();
        org.weixin4j.model.user.User info = weixinTemplate.user().info(fromUserName);
        System.out.println("nihao");
        User user = new User();
        user.setHeadUrl(info.getHeadimgurl());
        user.setUsername(info.getNickname());
        user.setSubscribeState(1);
        user.setOpenid(fromUserName);
        user.setPoints(0);
        user.setLvId(1);
        user.setRegistertime(new Date());
        try {
            userServiceImp.insertUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

  /* 取消订阅*/
    @Override
    public OutputMessage unSubscribe(UnSubscribeEventMessage unSubscribeEventMessage) {
        String fromUserName = unSubscribeEventMessage.getFromUserName();
        User user = new User();
        user.setSubscribeState(0);
        user.setOpenid(fromUserName);
        try {
            userServiceImp.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public OutputMessage qrsceneSubscribe(QrsceneSubscribeEventMessage qrsceneSubscribeEventMessage) {
        return null;
    }

    @Override
    public OutputMessage qrsceneScan(QrsceneScanEventMessage qrsceneScanEventMessage) {
        return null;
    }

    @Override
    public OutputMessage location(LocationEventMessage locationEventMessage) {
        return null;
    }

    @Override
    public OutputMessage click(ClickEventMessage clickEventMessage) {

        return null;
    }

    @Override
    public OutputMessage view(ViewEventMessage viewEventMessage) {
        return null;
    }

    @Override
    public OutputMessage scanCodePush(ScanCodePushEventMessage scanCodePushEventMessage) {
        return null;
    }

    @Override
    public OutputMessage scanCodeWaitMsg(ScanCodeWaitMsgEventMessage scanCodeWaitMsgEventMessage) {
        return null;
    }

    @Override
    public OutputMessage picSysPhoto(PicSysPhotoEventMessage picSysPhotoEventMessage) {
        return null;
    }

    @Override
    public OutputMessage picPhotoOrAlbum(PicPhotoOrAlbumEventMessage picPhotoOrAlbumEventMessage) {
        return null;
    }

    @Override
    public OutputMessage picWeixin(PicWeixinEventMessage picWeixinEventMessage) {
        return null;
    }

    @Override
    public OutputMessage locationSelect(LocationSelectEventMessage locationSelectEventMessage) {
        return null;
    }
}
